<template>
  <div class="app-container">
    <div class="search-container">
      <el-form-item label="" style="margin-left: 10px">
        <el-radio-group v-model="params.state" @change="stateRadioChange">
          <el-radio name="type" :value="0">已提交</el-radio>
          <el-radio name="type" :value="1">已指派</el-radio>
          <el-radio name="type" :value="2">已确认</el-radio>
        </el-radio-group>
      </el-form-item>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="feedbackId" width="80" label="编号" />
        <el-table-column label="状态">
          <template #default="scope">
            <div v-if="scope.row.state == 0">已提交</div>
            <div v-if="scope.row.state == 1">已指派</div>
            <div v-if="scope.row.state == 2">已确认</div>
          </template>
        </el-table-column>
        <el-table-column sortable prop="fdatetime" label="日期">
          <template #default="scope">
            {{ formatDate(new Date(scope.row.fdatetime)) }}
          </template>
        </el-table-column>
        <el-table-column label="时间">
          <template #default="scope">
            {{ formatTime(new Date(scope.row.fdatetime)) }}
          </template>
        </el-table-column>
        <el-table-column prop="province.pname" label="省份" />
        <el-table-column prop="city.cityName" label="城市" />
        <el-table-column label="照片">
          <template #default="scope">
            <el-image
              style="
                width: 100px;
                height: 100px;
                background-color: #fff;
                z-index: 9999;
              "
              :src="
                baseUrl +
                '/supervisor-api/file/imgId/' +
                scope.row.fileUploadImgId
              "
            />
          </template>
        </el-table-column>
        <el-table-column label="AQI预测等级">
          <template #default="scope">
            <el-button type="success" plain v-if="scope.row.aqi.aqiId == 1"
              >一</el-button
            >
            <el-button
              type="success"
              color="#baae00"
              plain
              v-if="scope.row.aqi.aqiId == 2"
              >二</el-button
            >
            <el-button
              type="success"
              color="#ffb000"
              plain
              v-if="scope.row.aqi.aqiId == 3"
              >三</el-button
            >
            <el-button
              type="success"
              color="#ff4600"
              plain
              v-if="scope.row.aqi.aqiId == 4"
              >四</el-button
            >
            <el-button
              type="success"
              color="#006bff"
              plain
              v-if="scope.row.aqi.aqiId == 5"
              >五</el-button
            >
            <el-button
              type="success"
              color="#b000ff"
              plain
              v-if="scope.row.aqi.aqiId == 6"
              >六</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="viewDetails(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px"
        background
        layout="prev, pager, next"
        :total="pageRes.totalRow"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </el-card>
    <el-dialog
      v-model="viewDetailsDialogFormVisible"
      title="详情查看"
      width="800"
    >
      <el-descriptions title="" :column="2" border>
        <el-descriptions-item label="状态">
          <div v-if="curRow.state == 0">已提交</div>
          <div v-if="curRow.state == 1">已指派</div>
          <div v-if="curRow.state == 2">已确认</div>
        </el-descriptions-item>
        <el-descriptions-item label="省份">{{
          curRow.province.pname
        }}</el-descriptions-item>
        <el-descriptions-item label="城市">{{
          curRow.city.cityName
        }}</el-descriptions-item>
        <el-descriptions-item label="详细地址">{{
          curRow.faddress
        }}</el-descriptions-item>
        <el-descriptions-item label="预估AQI等级">
          <el-button type="success" plain v-if="curRow.aqi.aqiId == 1"
            >优（一）</el-button
          >
          <el-button
            type="success"
            color="#baae00"
            plain
            v-if="curRow.aqi.aqiId == 2"
            >良（二）</el-button
          >
          <el-button
            type="success"
            color="#ffb000"
            plain
            v-if="curRow.aqi.aqiId == 3"
            >轻度污染（三）</el-button
          >
          <el-button
            type="success"
            color="#ff4600"
            plain
            v-if="curRow.aqi.aqiId == 4"
            >中度污染（四）</el-button
          >
          <el-button
            type="success"
            color="#006bff"
            plain
            v-if="curRow.aqi.aqiId == 5"
            >重度污染（五）</el-button
          >
          <el-button
            type="success"
            color="#b000ff"
            plain
            v-if="curRow.aqi.aqiId == 6"
            >严重污染（六）</el-button
          >
        </el-descriptions-item>
        <el-descriptions-item label="日期">
          {{ formatDate(new Date(curRow.fdatetime)) }}
        </el-descriptions-item>
        <el-descriptions-item label="时间">
          {{ formatTime(new Date(curRow.fdatetime)) }}
        </el-descriptions-item>
        <el-descriptions-item label="图片">
          <el-image
            style="
              width: 100px;
              height: 100px;
              background-color: #fff;
              z-index: 9999;
            "
            :src="
              baseUrl + '/supervisor-api/file/imgId/' + curRow.fileUploadImgId
            "
            :preview-src-list="[
              baseUrl + '/supervisor-api/file/imgId/' + curRow.fileUploadImgId,
            ]"
          />
        </el-descriptions-item>
        <el-descriptions-item
          v-if="curRow.state == 1 || curRow.state == 2"
          label="网格员姓名"
        >
          {{ curRow.grid.gname }}
        </el-descriptions-item>
        <el-descriptions-item
          v-if="curRow.state == 1 || curRow.state == 2"
          label="网格员电话"
        >
          {{ curRow.grid.gphone }}
        </el-descriptions-item>
        <el-descriptions-item v-if="curRow.state == 2" label="确认AQI等级">
          <el-button type="success" plain v-if="curRow.verify.aqiVerId == 1"
            >优（一）</el-button
          >
          <el-button
            type="success"
            color="#baae00"
            plain
            v-if="curRow.verify.aqiVerId == 2"
            >良（二）</el-button
          >
          <el-button
            type="success"
            color="#ffb000"
            plain
            v-if="curRow.verify.aqiVerId == 3"
            >轻度污染（三）</el-button
          >
          <el-button
            type="success"
            color="#ff4600"
            plain
            v-if="curRow.verify.aqiVerId == 4"
            >中度污染（四）</el-button
          >
          <el-button
            type="success"
            color="#006bff"
            plain
            v-if="curRow.verify.aqiVerId == 5"
            >重度污染（五）</el-button
          >
          <el-button
            type="success"
            color="#b000ff"
            plain
            v-if="curRow.verify.aqiVerId == 6"
            >严重污染（六）</el-button
          >
        </el-descriptions-item>
        <el-descriptions-item v-if="curRow.state == 2" label="确认SO2浓度">
          {{ curRow.verify.so2 }} ug/m3
        </el-descriptions-item>
        <el-descriptions-item v-if="curRow.state == 2" label="确认CO浓度">
          {{ curRow.verify.co }} ug/m3
        </el-descriptions-item>
        <el-descriptions-item v-if="curRow.state == 2" label="确认PM浓度">
          {{ curRow.verify.pm }} ug/m3
        </el-descriptions-item>
        <el-descriptions-item v-if="curRow.state == 2" label="确认时间">
          {{ formatDateTime(new Date(curRow.verify.verDatetime)) }}
        </el-descriptions-item>
        <el-descriptions-item label="反馈信息">
          <el-tag size="small">{{ curRow.aqi.aqiDesc }}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="viewDetailsDialogFormVisible = false"
          >
            返回
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import FeedbackAPI from "@/api/feedback";
import { ElMessage } from "element-plus";

const params: any = ref({
  aqiPreId: null,
  beginNum: 0,
  fcity: null,
  fdatetime: null,
  fprovince: null,
  pageNum: 1,
  pageSize: 4,
  state: 0,
});

const viewDetailsDialogFormVisible = ref(false);

const curRow: any = ref({});

const pageRes = ref({
  totalPage: 0,
  totalRow: 0,
});

const baseUrl = import.meta.env.VITE_APP_BASE_API;

const tableData: any = ref([]);

function reqFeedbackList() {
  FeedbackAPI.feedbackList(params.value).then((res) => {
    console.log(res);
    tableData.value = [];
    pageRes.value.totalPage = res.totalPage;
    pageRes.value.totalRow = res.totalRow;
    let i = 1;
    for (const v of res.feedbackList) {
      v.order = i++;
      tableData.value.push(v);
    }
  });
}

function pageChange(v: any) {
  console.log(v);
  console.log();
  params.value.pageNum = v;
  reqFeedbackList();
}

function pad(num: any) {
  return num.toString().padStart(2, "0");
}

function formatDateTime(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(
    second
  )}`;
}

function formatDate(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${pad(month)}-${pad(day)}`;
}

function formatTime(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${pad(hour)}:${pad(minute)}:${pad(second)}`;
}

function viewDetails(row: any) {
  curRow.value = row;
  viewDetailsDialogFormVisible.value = true;
}

onMounted(() => {
  reqFeedbackList();
});

function stateRadioChange(v: any) {
  console.log(v);
  reqFeedbackList();
}
</script>

<style scoped>
/* 去除el-card的边框 */
.table-container .el-card {
  border: none; /* 去除边框 */
  box-shadow: none; /* 去除阴影 */
}

/* 去除内边距，添加如下样式 */
.table-container .el-card.is-always-shadow {
  -webkit-box-shadow: none;
  box-shadow: none;
}
</style>
